import json

import scrapy
import os
from os import path

from scrapy.loader import ItemLoader

from ts.items.Movie import Movie


class QuotesSpider(scrapy.Spider):
    name = "news"

    def start_requests(self):
        urls = [
            'https://www.xwb2uooia5o59avwxb.com/page/1',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        items = response.css('div.item a')

        for item in items:
            item_loader = ItemLoader(item=Movie(), selector=item)

            dates = item.css('date::text').extract()
            url = item.css('::attr(href)').extract_first()

            item_loader.add_value('url', url)
            item_loader.add_value('code', dates[0])
            item_loader.add_css('temp_img', 'div.photo-frame img::attr(src)')
            item_loader.add_css('title', 'div.photo-info span::text')
            item_loader.add_value('pubdate', dates[1])

            if url is not None:
                yield scrapy.Request(url, meta={'loader': item_loader}, callback=self.parse_detail)

    def parse_detail(self, response):
        all_p = response.css('div.info p')
        if len(all_p) >= 7:
            types = all_p[7].css('a::text').extract()
            loader = response.meta['loader']
            if loader:
                loader.add_value('types', types)

            yield loader.load_item()
